package com.example.binpacking.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.example.binpacking.mapper.InterviewerMapper;
import com.example.binpacking.mapper.SysmasterMapper;
import com.example.binpacking.mapper.TeacherMapper;
//import com.example.binpacking.model.*;
import com.example.binpacking.model.*;
import com.example.binpacking.model.dto.LoginRequest;
import com.example.binpacking.service.AuthService;
import com.example.binpacking.util.DataFormatter;
import com.example.binpacking.util.JWTUtil;
import com.google.gson.JsonObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/pelicanpsyc")
public class AuthController {
    @Autowired
    private AuthService authService;

    @Autowired
    private InterviewerMapper interviewerMapper;
    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private SysmasterMapper sysmasterMapper;
    @ApiOperation("登录和注册式登录")
    @PostMapping("/api/login")
    public Result login(@RequestBody LoginRequest request) {

        if ("reg".equals(request.getType().toLowerCase())) {
//            if (request == null) {
////                logger.error("LoginRequest is null");
//                return Result.fail(400,"400", "Bad request");
//            }
//
//            String type = request.getType();
//            String loginid = request.getLoginid();
//            String password = request.getPassword();
//            String usertype = request.getUsertype();
//
//            if (type == null || loginid == null || password == null || usertype == null) {
////                logger.error("LoginRequest contains null fields");
//                return Result.fail(400,"400", "Bad request");
//            }

            boolean success = authService.register(request.getLoginid(), request.getPassword(), request.getUsertype());
            Interviewer i =interviewerMapper.selectByMaxOrderId();
            Teacher t = teacherMapper.selectByMaxOrderId();
            Sysmaster s = sysmasterMapper.selectByMaxOrderId();
//            Map<String, Object> formattedDatai = DataFormatter.formatData(i);
//            Map<String, Object> formattedDatat = DataFormatter.formatData(t);
//            Map<String, Object> formattedDatas = DataFormatter.formatData(s);
            if (success) {

                if(i!=null){
//                    String encryptedPassword = DigestUtils.md5Hex(i.getPassword());
//                    i.setPassword(encryptedPassword);
                    System.out.println(i.getPassword());
                    Map<String, Object> formattedDatai = DataFormatter.formatDataByNull(i);
                return Result.success(200,"y",Collections.singletonList(formattedDatai));}
                if(t!=null){
                    return Result.success(200,"y",Collections.singletonList(t));}
                if(s!=null){
                    return Result.success(200,"y",Collections.singletonList(s));}
            } else {
                return Result.fail(200,"n",Collections.emptyList());
            }
        } else if ("login".equals(request.getType().toLowerCase())) {
            Object user = authService.login(request.getLoginid(), request.getPassword(), request.getUsertype());
            System.out.println(user);
            if (user != null) {
                System.out.println("yyyyyyyyyyyyyyy");
                //Object formattedData = DataFormatter.formatData(user);
                System.out.println("yyyyyyyyyyyyyyy22");
                String jsonString = JSONObject.toJSONString(user, SerializerFeature.WriteMapNullValue);
                JSONObject reyturn_data = JSONObject.parseObject(jsonString);
                //reyturn_data.put(user,user);
                for (String key : reyturn_data.keySet()) {
                    if (reyturn_data.get(key) == null) {
                        reyturn_data.put(key, "");
                    }
                }
                return Result.success(200,"y", Collections.singletonList(reyturn_data));
            } else {
                return Result.fail(200,"n",Collections.emptyList());
            }
        }
        return Result.fail("no login and reg");
    }

    //    @PostMapping("/api/login")
//    public Result login(@RequestParam BigDecimal loginid, @RequestParam String password) {
//        String strValue = loginid.toString();
//        String token = JWTUtil.createToken(strValue,password);
//        String encryptedPassword = DigestUtils.md5Hex(password);
//        List<Interviewer> list = interviewerService.findByLoginIdAndPassword(loginid,encryptedPassword);
//
//        HashMap map = new HashMap<>();
//        map.put("token",token);
//        map.put("data",list);
//        return Result.success(map);
//    }
}
